import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';

import '../../net/post_request.dart';
import '../../net/response/patrol/patrol_detail_data.dart';
import '../../net/response/patrol/patrol_record_detail_data.dart';

class PatrolDetailViewModel extends BaseViewModel {
  final String code;
  final int? id;
  final PatrolRecordDetailData? item;

  PatrolDetailViewModel(BuildContext context,
      {required this.code, this.id, this.item})
      : super(context);

  var patrolHandleStatus = "正常";
  var patrolHandleStatusId = "2";

  PatrolDetailData? detailData;

  var detailList = <PatrolDetailData>[];

  var fileStringLit = <String>[];

  String feedback = '';

  Widget selectHandleStatusWidget() {
    var items = <PickerDataWithIdModel>[
      PickerDataWithIdModel(pickName: '正常', pickId: '2'),
      PickerDataWithIdModel(pickName: '异常', pickId: '3')
    ];
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: items.map((e) {
        return CommonText(e.pickName,
                textColor:
                    e.name == patrolHandleStatus ? Colors.white : Colors.black)
            .addToContainer(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                decoration: BoxDecoration(
                    borderRadius: 5.borderRadius,
                    color: e.name == patrolHandleStatus
                        ? Colours.app_main
                        : Colors.white,
                    border: Border.all(
                        width: 1,
                        color: e.name == patrolHandleStatus
                            ? Colours.app_main
                            : Colors.grey)))
            .onTap(() {
          patrolHandleStatus = e.name;
          patrolHandleStatusId = e.pickId??"";
          notifyListeners();
        });
      }).toList(),
    );
  }

  Future<void> submitPatrol({bool isResign = false}) async {
    showLoadingDialog(msg: '提交中...');
    try {
      var bean = await PostRequest.submitPatrol(
          detailData!.id.toString(), patrolHandleStatusId, feedback,
          isSupplementaryCard: isResign ? 1 : null);
      hideDialog();
      if ("0" == bean['code']) {
        showToast('提交成功');
        sendMessage(SendMessageEvent(MessageConfig.REFRESH_PATROL));
        finish();
        // getCodeData();
      } else {
        showToast(bean['msg']);
      }
    } catch (e) {
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> uploadAbnormal() async {
    showLoadingDialog(msg: '提交中...');
    try {
      var bean = await PostRequest.uploadAbnormal(
          detailData!.id.toString(), patrolHandleStatusId, feedback);
      hideDialog();
      if ("0" == bean['code']) {
        showToast('提交成功');
        sendMessage(SendMessageEvent(MessageConfig.REFRESH_PATROL));
        finish();
      } else {
        showToast(bean['msg']);
      }
    } catch (e) {
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> getCodeData() async {
    showLoadingState();
    try {
      var data = await PostRequest.scanGetPatrolDetail(code,
          executeTaskId: id.toStringOrNull());
      if ("0" == data['code']) {
        if (data['data'] != null) {
          detailList = (data['data'] as List ?? [])
              .map((e) => PatrolDetailData.fromJson(e))
              .toList();
          detailData = detailList[0];
          showContent();
          notifyListeners();
        } else {
          showErrorState('未获取到数据');
        }
      } else {
        showErrorState(data['msg']);
      }
    } catch (e) {
      print(e);
      showErrorState(NetErrorUtils.getNetError(e));
    }
  }

  void changeDetail() {
    DataPickerView(context, detailList, onSubmitListener: (p1, p2, p3) {
      detailData = detailList[p1];
      notifyListeners();
    }).show();
  }

  @override
  void onCreated() {
    super.onCreated();
    if (item == null) {
      getCodeData();
    } else {
      detailData = PatrolDetailData.fromJson(item!.toJson());
      notifyListeners();
    }
  }
}
